package gameObjects;

import java.text.DecimalFormat;
import java.text.NumberFormat;

import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.scene.Text;
import com.jme.scene.state.RenderState;
import com.jme.system.DisplaySystem;
import audio.SFX;

public class UpdateCoins extends Node {
	private static final long serialVersionUID = 2704102210L;
	private  Text 			coin;
    private  StringBuffer	strBuf;
    private  String			strCoin;
    private  int 			height = DisplaySystem.getDisplaySystem().getHeight();
    private  SFX			plin = new SFX("sound/point.ogg", false, 0.2f, 0.2f, false);
    private  float			ratio = 1.62f;
    private  float 			posX;
    private  boolean		isHuman;
    private  int			coins;
    
	public UpdateCoins(boolean isHumanPlayer)
	{
		
		super("COINS");
		posX = DisplaySystem.getDisplaySystem().getWidth() /ratio;
		this.isHuman=isHumanPlayer;
		this.coins=0;
		NumberFormat formatter = new DecimalFormat("000000"); 
		if(isHuman)
			strCoin = formatter.format(0);
		else
			strCoin = "";
		
		strBuf=new StringBuffer(10);
		strBuf.setLength(0);
		strBuf.append(strCoin);
		
		coin = new Text();
		coin = Text.createDefaultTextLabel( "COIN label" );
		coin.setTextColor(ColorRGBA.yellow.clone());
        coin.setCullHint(CullHint.Never);
        coin.setTextureCombineMode(TextureCombineMode.Replace);
		coin.setRenderQueueMode(Renderer.QUEUE_ORTHO);
		coin.setLightCombineMode(Spatial.LightCombineMode.Off);
		coin.setLocalTranslation(new Vector3f(posX, height - 20, 0));
		coin.print(strBuf);
		
		this.setName("COINS_NODE");
        this.setRenderState(this.getRenderState(RenderState.StateType.Blend));
        this.setRenderState(this.getRenderState(RenderState.StateType.Texture));
        this.attachChild(coin);
        this.setCullHint(CullHint.Never);
	}

	public void update(int coins)
	{
		this.coins=this.coins+coins;
		if(isHuman)
		{
			NumberFormat formatter = new DecimalFormat("000000"); 
			strCoin = formatter.format(this.coins);    
			coin.setLocalTranslation(new Vector3f(posX, height-20, 0));
	        strBuf.setLength(0);
	        strBuf.append(strCoin);
	        coin.print(strCoin);
	        play();
		}
		else
		{
			strBuf.setLength(0);
	        strBuf.append("");
	        coin.print(strBuf);
		}
	}
	
	private void play()
	{
		plin.Play();
	}

}
